<?php
//阿里云oss存储
namespace app\common\controller;

use JohnLui\AliyunOSS\AliyunOSS;
use Aliyun\OSS\Exceptions\OSSException;

error_reporting(0);
class Ossaliyun
{
    public $accessKeyId = "";
    public $accessKeySecret = "";
    public $endpoint = "";
    public $bucket= "";
    public function __construct()
    {
        $this->endpoint = Config('alioss.ossServer');
        $this->accessKeyId = Config('alioss.AccessKeyId');
        $this->accessKeySecret = Config('alioss.AccessKeySecret');
        $this->bucket = Config('alioss.BucketName');
    }
    public function uploadOssFile($ossKey, $filePath)
    {
        try{
            $ossClient = new AliyunOSS($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $ossClient->setBucket($this->bucket);
            $res = $ossClient->uploadFile($ossKey, $filePath);
            dump($res);
            exit;
        } catch(OssException $e) {
            return false;
        }
//        return $res;
    }

    /**
     * oss判断文件是否存在
     * @param $object 要判断的文件
     */
    public function checkFileExist($object)
    {
        try{
            $ossClient = new AliyunOSS($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $exist = $ossClient->getObjectMeta($this->bucket, $object);
        } catch(OssException $e) {
            return false;
        }
        return $exist;
    }

    /**
     * oss 删除多个文件
     * @param $String array  多个文件
     * @return bool
     */
    public function deleteFiles($object)
    {
        try{
            $ossClient = new AliyunOSS($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $ossClient->deleteObject($this->bucket, $object);
        } catch(OssException $e) {
            return false;
        }
        return true;
    }

    /**
     * 获取指定文件件下的文件列表
     * @param string $prefix   指定目录
     * @param string $nextMarker  从那个文件开始（不包含）（获取文件夹下所有文件的话，值与$prefix相同）
     * @param string $delimiter  对文件名称进行分组的一个字符。CommonPrefixes是以delimiter结尾，并有共同前缀的文件集合。
     * @param string $maxkeys  列举文件的最大个数。默认为100，最大值为1000。
     * @return bool | array 返回文件列表
     */
    public function listObject($prefix = '', $nextMarker = '', $delimiter= '', $maxkeys = '')
    {
        try {
            $options = array(
                'prefix' => $prefix,
                'marker' => $nextMarker,
                'delimiter' => $delimiter,
                'max-keys' => $maxkeys,
            );
            $ossClient = new AliyunOSS($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $listObjectInfo = $ossClient->getAllObjectKeyWithPrefix($this->bucket, $options);
        } catch (OssException $e) {
            return false;
        }
//		$nextMarker = $listObjectInfo->getNextMarker();
        $listObject = $listObjectInfo->getObjectList();
//		$listPrefix = $listObjectInfo->getPrefixList();

        if (!empty($listObject)) {
            $list = array();
            foreach ($listObject as $objectInfo) {
                $list[] = $objectInfo->getKey();
            }
            return $list;
        }else{
            return false;
        }
    }
}